package com.yh.test.web.controller;

import com.google.openrtb.OpenRtb;
import com.google.openrtb.Test;
import com.yh.test.pojo.domain.Admin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.util.Arrays;
import java.util.List;

/**
 * Created by songyu on 2017/2/9.
 */
@Controller
@RequestMapping("/protobuf")
public class ProtobufController {

    @RequestMapping(value = "/protobuf_adx2", method = RequestMethod.POST)
    public void protobufAdx2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        InputStream is = request.getInputStream();
        OpenRtb.BidRequest bid = OpenRtb.BidRequest.parseFrom(is);
        System.out.println("接收到的消息:");
        System.out.println(bid);

        OpenRtb.BidResponse res = OpenRtb.BidResponse.newBuilder().
                setId("1234567890")
                .addSeatbid(OpenRtb.BidResponse.SeatBid.newBuilder()
                        .addBid(OpenRtb.BidResponse.SeatBid.Bid.newBuilder()
                                .setId("1")
                                .setImpid("102")
                                .setPrice(9.43)
                                .setAdid("314")
                                .setNurl("http://adserver.com/winnotice?impid=102")
                                .setAdm(
                                        "%3C!DOCTYPE%20html%20PUBLIC%20%5C%22-"
                                                + "%2F%2FW3C%2F%2FDTD%20XHTML%201.0%20Transitional%2F%2FEN%5C%22%20%5C%22htt"
                                                + "p%3A%2F%2Fwww.w3.org%2FTR%2Fxhtml1%2FDTD%2Fxhtml1-"
                                                + "transitional.dtd%5C%22%3E%3Chtml%20xmlns%3D%5C%22http%3A%2F%2Fwww.w3.org%2F1"
                                                + "999%2Fxhtml%5C%22%20xml%3Alang%3D%5C%22en%5C%22%20lang%3D%5C%22en%5C%22"
                                                + "%3E...%3C%2Fhtml%3E")
                                .addAdomain("advertiserdomain.com")
                                .setIurl("http://adserver.com/pathtosampleimage")
                                .setCid("campaign111")
                                .setCrid("creative112")
                                .addAllAttr(Arrays.asList(OpenRtb.CreativeAttribute.ANNOYING, OpenRtb.CreativeAttribute.AUDIO_AUTO_PLAY)))
                        .setSeat("512"))
                .setBidid("abc1123")
                .setCur("USD")
                .build();
        response.setContentType("application/x-protobuf");
        OutputStream outputStream = response.getOutputStream();
        res.writeTo(outputStream);
        outputStream.flush();
        outputStream.close();
    }
}
